﻿using Klear.Library.Models;

namespace Klear.Library.Services;

public class CategoryService : ICategoryService
{
    private IDatabaseService _databaseService;
    public CategoryService(IDatabaseService databaseService) {
        _databaseService = databaseService;
    }    
    public async Task<IList<CategoryModel>> GetCategoriesAsync() {
        return await _databaseService.GetCategoriesAsync();
    }
    public async Task<IList<CategoryModel>> GetExpenseCategoriesAsync() {
        return await _databaseService.GetExpenseCategoriesAsync();
    }
    public async Task<IList<CategoryModel>> GetIncomeCategoriesAsync() {
        return await _databaseService.GetIncomeCategoriesAsync();
    }
    public async Task AddCategoryAsync(CategoryModel categoryModel) {
        await _databaseService.AddCategoryAsync(categoryModel);
    }

    public async Task UpdateCategoryAsync(CategoryModel categoryModel) {
        await _databaseService.UpdateCategoryAsync(categoryModel);
    }

    public async Task DeleteCategoryAsync(int id) {
        CategoryModel categoryModel = new CategoryModel();
        var categories = await _databaseService.GetCategoriesAsync();
        bool find = false;
        foreach (var category in categories) {
            if (category.CategoryId == id) {
                categoryModel = category;
                find = true;
            }
        }
        if (!find) {
            return;
        }
        await _databaseService.DeleteCategoryAsync(categoryModel);
    }
}